package com.tanhua.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Service;
import com.mongodb.client.result.UpdateResult;
import com.tanhua.dubbo.server.pojo.Comment;
import com.tanhua.dubbo.server.pojo.MessagesPojo;
import com.tanhua.dubbo.server.pojo.PageResult;
import com.tanhua.dubbo.server.pojo.Totals;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;

@Service(version = "1.0.0")
public class MessageApiImpl implements MessageApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //动态通过
    /*
    * id 动态id,userId用户
    * */
    @Override
    public boolean messagesPass(Integer userId,String id, String state) {
        try {
            Query query=Query.query(Criteria.where("id").is(new ObjectId(id)));
            Update update=Update.update("state","5").set("code","5");
            mongoTemplate.updateFirst(query,update, MessagesPojo.class,"messages");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    //消息拒绝
    @Override
    public boolean messagesReject(Integer userId,String id, String state) {
        try {
            Query query=Query.query(Criteria.where("id").is(new ObjectId(id)));
            Update update=Update.update("state","4").set("code","4");
            mongoTemplate.updateFirst(query,update, MessagesPojo.class,"messages");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }


    //撤销
    @Override
    public boolean messageRevocation(Integer userId, String id, String state) {
        try {
            Query query=Query.query(Criteria.where("id").is(new ObjectId(id)));
            Update update=Update.update("state","3").set("code","3");
            mongoTemplate.updateFirst(query,update, MessagesPojo.class,"messages");
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //根据动态ids[]查动态详情
    public List<MessagesPojo> queryMessageByIds(String[] ids){
        Query query1 = Query.query(Criteria.where("id").in(ids));
        List<MessagesPojo> messages = mongoTemplate.find(query1, MessagesPojo.class, "messages");
        return messages;
    }

    //根据动态id查动态详情
    @Override
    public MessagesPojo queryMessageById(String id) {
        Query query1 = Query.query(Criteria.where("id").is(id));
        MessagesPojo messages = mongoTemplate.findOne(query1, MessagesPojo.class, "messages");
        return messages;
    }

    //条件查询
    @Override
    public List<MessagesPojo> queryByQuery(String userId,Integer page,Integer pageSize,String id, String sd, String ed, String sortProp, String sortOrder, String state) {
        if(StringUtils.equals("reportCount",sortProp)){
            sortProp = "createDate";
        }
        if(StringUtils.equals("forwardingCount",sortProp)){
            sortProp = "createDate";
        }
        //判断条件查询,若查询参数都为null,则返回所有;
        Query query1=new Query();
        if ("all".equals(state) && StringUtils.isNotEmpty(state)){
            query1=Query.query(Criteria.where("state").is(state));
        }
        //是否条件查询
        if (StringUtils.isNotEmpty(id) && "-1".equals(sd) && "-1".equals(ed)){
             query1 = Query.query(Criteria.where("id").is(id).and("state").is(state));
        }else if (StringUtils.isNotEmpty(id) && !"-1".equals(sd) && !"-1".equals(sd)){
             query1 = Query.query(Criteria.where("id").is(id).and("createDate").gt(sd).and("createDate").lt(ed).and("state").is(state));
        }else if (StringUtils.isEmpty(id) && StringUtils.isNotEmpty(userId)){
            query1=Query.query(Criteria.where("userId").is(userId));
        }
        //按规则排序
        Query query2=null;
        if ("descending".equals(sortOrder)){
            PageRequest pageRequest=new PageRequest(page-1,pageSize,Sort.by(Sort.Order.desc("updateDate"),Sort.Order.desc(sortProp)));
            query2=query1.with(pageRequest);
        }else{
            PageRequest pageRequest=new PageRequest(page-1,pageSize,Sort.by(Sort.Order.desc("updateDate"),Sort.Order.asc(sortProp)));
            query2=query1.with(pageRequest);
        }

        List<MessagesPojo> messages = mongoTemplate.find(query2, MessagesPojo.class,"messages");
        return messages;
    }

    //统计数量
    @Override
    public long queryCount() {
        Query query=new Query();
        long count =mongoTemplate.count(query, "messages");
        return count;
    }

    //查询评论数量
    @Override
    public Long queryCommentCount(String publishId, Integer type) {
        Query query=new Query(Criteria.where("publishID").is(new ObjectId(publishId))
                .and("commentType").is(type));
        return  mongoTemplate.count(query, Comment.class);
    }

    //置顶
    @Override
    public Boolean topMessage(String id) {
        Query query = Query.query(Criteria.where("pid").is(id));
        Update update=Update.update("updateDate",System.currentTimeMillis());
        UpdateResult quanzi_publish = mongoTemplate.updateFirst(query, update, MessagesPojo.class, "messages");
        if (quanzi_publish.getMatchedCount()==0L){
            return false;
        }
        return true;
    }

    //取消置顶
    @Override
    public Boolean unTopMessage(String id) {
        Query query = Query.query(Criteria.where("pid").is(id));
        Update update=Update.update("updateDate",0);
        UpdateResult quanzi_publish = mongoTemplate.updateFirst(query, update, MessagesPojo.class, "messages");
        if (quanzi_publish.getMatchedCount()==0L){
            return false;
        }
        return true;
    }

    //分类
    @Override
    public List<Totals> queryCountCode() {
        //查询分类信息   total
        //全部
        Query query2=new Query();
        Integer value1 = (int)mongoTemplate.count(query2,"messages");
        Totals totals2=new Totals("全部","all",value1);
        //待审核
        Query query3=Query.query(Criteria.where("state").is("3"));
        Integer value2 = (int)mongoTemplate.count(query3,"messages");
        Totals totals3=new Totals("待审核","3",value2);
        //已通过
        Query query4=Query.query(Criteria.where("state").is("5"));
        Integer value3 = (int)mongoTemplate.count(query4,"messages");
        Totals totals4=new Totals("已通过","5",value3);
        //已驳回
        Query query5=Query.query(Criteria.where("state").is("4"));
        Integer value4 = (int)mongoTemplate.count(query5,"messages");
        Totals totals5=new Totals("已驳回","4",value4);
        List<Totals> totalsList=new ArrayList<>();
        totalsList.add(totals2);
        totalsList.add(totals3);
        totalsList.add(totals4);
        totalsList.add(totals5);
        return totalsList;
    }

}
